package com.ocean.service.library.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ocean.dao.library.BookDao;
import com.ocean.entity.library.Book;
import com.ocean.service.library.BookService;
import com.ocean.vo.EchartData;
import com.ocean.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Project：SchoolManage
 * Package：com.ocean.service.library.impl
 * Author:zy
 * Time:2023/3/3
 */

@Service
public class BookServiceImpl implements BookService {
    @Autowired
    private BookDao bookDao;

    @Override
    public R save(Book book) {
        if (bookDao.insert(book) > 0) {
            return R.ok();
        } else {
            return R.fail("插入图书失败");
        }
    }

    @Override
    public R queryPage(int page, int size,String name,String author) {
        PageHelper.startPage(page, size);
        PageInfo<Book> pageInfo = new PageInfo<>(bookDao.selectAllByCondition(name,author));
        return R.ok(pageInfo);
    }

    @Override
    public R delete(int id) {
        if(bookDao.delete(id)>0){
            return R.ok();
        }else {
            return R.fail();
        }
    }

    @Override
    public R update(Book book) {
        return R.ok(bookDao.update(book));
    }

    @Override
    public R selectIdByName(String name) {
        return R.ok(bookDao.selectIdByName(name));
    }

    @Override
    public R importData(MultipartFile file) throws IOException {
        if(!file.isEmpty()){
            List<Book>list = EasyExcel.read(file.getInputStream(), Book.class, new ReadListener() {
                @Override
                public void invoke(Object o, AnalysisContext analysisContext) {

                }

                @Override
                public void doAfterAllAnalysed(AnalysisContext analysisContext) {

                }
            }).doReadAllSync();

            if(bookDao.insertBatch(list)>0){
                return R.ok();
            }
        }

        return R.fail();
    }

    @Override
    public void exportData(HttpServletResponse response) throws IOException {

        response.setHeader("Content-Disposition", "attachment;filename=book_" + System.currentTimeMillis()+".xlsx");

        List<Book> list = bookDao.selectAll();

        EasyExcel.write(response.getOutputStream(),Book.class).sheet("自动导出").doWrite(list);


    }

    @Override
    public R queryEcharts() {
        List<EchartData> list = bookDao.selectechart();
        Map<String, ArrayList> map = new HashMap<>();
        map.put("name",new ArrayList());
        map.put("nums",new ArrayList());

        for (int i = 0; i < list.size(); i++) {
            String name = list.get(i).getName();
            map.get("name").add(name);
            int nums = list.get(i).getNums();
            map.get("nums").add(nums);

        }
        return R.ok(map);

    }

}
